<template>
	<view>
		<img src="/static/login/jiaopian.png" class="bg" />
		<img src="/static/login/logo.png" class="logo" />
		<view class="btn">
			<button open-type="getUserInfo" @click="login" style="position: absolute;width: 100%;height: 100%;opacity: 0;"></button>
			<uni-icons type="weixin" color="#fff" size="30"></uni-icons>
			<text class="btn_text">微信一键登陆</text>
		</view>
	</view>
</template>

<script>
import Login from '../../utils/login.js';
import Route from '../../utils/route.js';
export default {
	data() {
		return {
			openid: '',
			session_key: '',
			parent_id: ''
		};
	},
	async onLoad() {
		// 获取邀请人ID
		this.parent_id = uni.getStorageSync('parent_id');
		// 小程序获取Openid
		// #ifdef MP-WEIXIN
		let res = await Route.login_wechat_openid({
			code: await Login.getCode()
		});
		this.openid = res.openid;
		this.session_key = res.session_key;
		uni.setStorageSync('openid', res.openid);
		uni.setStorageSync('session_key', res.session_key);
		// #endif
	},
	methods: {
		login() {
			this.helper.preventActive(async () => {
				uni.showLoading({
					title: '正在登录'
				});
				uni.getUserProfile({
					desc: '登录',
					success: async e => {
						let data = {
							openid: this.openid,
							nickName: e.userInfo.nickName,
							avatarUrl: e.userInfo.avatarUrl,
							formType: 1,
							parent_id: this.parent_id || ''
						};
		
						let res = await Route.login_wechat_userinfo(data);
						uni.setStorageSync('userInfo', res.info);
						uni.navigateBack();
					},
					fail: res => {
						uni.hideLoading();
					},
					complete:res=>{
						uni.hideLoading()
					}
				});
			});
		}
	}
};
</script>

<style lang="scss">
page {
	background: $template-background;
}
.bg {
	position: fixed;
	top: 0;
	left: 0;
	width: 750rpx;
	height: 100vh;
}
.logo {
	position: absolute;
	left: 0;
	right: 0;
	top: 8vh;
	margin: auto;
	width: 300rpx;
	height: 300rpx;
}
.btn {
	position: absolute;
	left: 0;
	right: 0;
	bottom: 333rpx;
	margin: auto;
	background: #04be02;
	color: #fff;
	font-size: 32rpx;
	font-weight: 600;
	width: 490rpx;
	height: 81rpx;
	text-align: center;
	line-height: 81rpx;
	border-radius: 33rpx;
	display: flex;
	justify-content: center;
	&_text {
		margin-left: 8rpx;
	}
}
</style>
